<template>
  <el-dialog
      :model-value="visible"
      title="编辑会话"
      @update:model-value="val => $emit('update:visible', val)"
  >
    <el-form :model="localSession">
      <el-form-item label="会话名称">
        <el-input v-model="localSession.name" />
      </el-form-item>
      <!-- 其他字段 -->
    </el-form>

    <template #footer>
      <el-button @click="$emit('update:visible', false)">取消</el-button>
      <el-button
          type="primary"
          @click="handleConfirm"
      >
        确认修改
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import {ElMessage} from "element-plus";

const props = defineProps({
  visible: Boolean,
  session: Object
})

const emit = defineEmits(['update:visible', 'confirm'])

// 创建本地副本
const localSession = ref({})

watch(() => props.session, (newVal) => {
  localSession.value = { ...newVal }
}, { immediate: true })

const handleConfirm = () => {
  if (!localSession.value.name?.trim()) {
    return ElMessage.error('会话名称不能为空')
  }
  emit('confirm', localSession.value)
  emit('update:visible', false)
}
</script>
